package com.toogu.mazhangmen.ui.main;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.toogu.mazhangmen.bean.EquipmentsBean;
import com.toogu.mazhangmen.bean.QATypeBean;
import com.toogu.mazhangmen.ui.monitor.MonitorActivity;
import com.toogu.mazhangmen.util.AlarmUtil;
import com.toogu.mazhangmen.util.EquipmentUtil;
import com.toogu.mazhangmen.util.NetWorkUtil;
import com.toogu.mazhangmen.util.PopUtil;
import com.toogu.mazhangmen.util.ToastUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;


public class MonitorWithButtonAdapter extends RecyclerView.Adapter<MonitorWithButtonAdapter.ViewHolder>{
    private Activity activity;
    private List<EquipmentsBean> equipmentsBeanList;

    private List<ViewHolder> list;

    public MonitorWithButtonAdapter(Activity activity, List<EquipmentsBean> equipmentsBeanList){
        this.activity=activity;
        this.equipmentsBeanList=equipmentsBeanList;
        list = new ArrayList<>();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        private MonitorWithButton monitorWithButton;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            monitorWithButton = (MonitorWithButton) itemView;
        }
    }

    @Override
    public void onViewRecycled(@NonNull MonitorWithButtonAdapter.ViewHolder holder) {
        super.onViewRecycled(holder);
        holder.monitorWithButton.monitorUtil.destroyMonitor();
        list.remove(holder);
    }

    @Override
    public void onViewDetachedFromWindow(@NonNull MonitorWithButtonAdapter.ViewHolder holder) {
        super.onViewDetachedFromWindow(holder);
        holder.monitorWithButton.monitorUtil.stopMonitor();
    }

    @Override
    public void onViewAttachedToWindow(@NonNull MonitorWithButtonAdapter.ViewHolder holder) {
        super.onViewAttachedToWindow(holder);
        holder.monitorWithButton.monitorUtil.startMonitor();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        final View view = new MonitorWithButton(activity);
        final MonitorWithButtonAdapter.ViewHolder holder = new MonitorWithButtonAdapter.ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        EquipmentsBean equipmentsBean = equipmentsBeanList.get(position);
        holder.monitorWithButton.setInit(activity, equipmentsBean, new MonitorWithButton.ClickLister() {
            boolean isTalk = false;
            @Override
            public void buttonTalk(TextView textView) {
                if(holder.monitorWithButton.getMonitorUtil().getIsConnected()){
                    PopUtil.showTalk(activity,holder.monitorWithButton.monitorUtil);
//                    if(!isTalk){
//                        textView.setText("关闭对话");
//                        holder.monitorWithButton.getMonitorUtil().openTalk();
//                    }
//                    else{
//                        textView.setText("对话");
//                        holder.monitorWithButton.getMonitorUtil().closeTalk();
//                    }
//                    isTalk=!isTalk;
                }
                else
                    Toast.makeText(activity, "监控未连接", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void buttonAlarm(TextView textView) {
                if (holder.monitorWithButton.monitorUtil.getIsConnected())
                    if(AlarmUtil.getInstance().setLanAlarm(equipmentsBean,true)){
                        ToastUtil.getToastUtil().add(equipmentsBean.getName()+"打开警报成功");
                    }else{
                        Toast.makeText(activity, "打开警报失败", Toast.LENGTH_SHORT).show();
                    }
                else
                    Toast.makeText(activity, "监控未连接", Toast.LENGTH_SHORT).show();
            }
            @Override
            public void closeDoor(String doorId) {
                if (holder.monitorWithButton.monitorUtil.getIsConnected())
                    NetWorkUtil.getNetWorkUtil().halfOpen(activity, doorId, false, equipmentsBean.getName());
                else
                    Toast.makeText(activity, "监控未连接", Toast.LENGTH_SHORT).show();
            }
            @Override
            public void openDoor(String doorId) {
                if (holder.monitorWithButton.monitorUtil.getIsConnected())
                    NetWorkUtil.getNetWorkUtil().halfOpen(activity, doorId, false, equipmentsBean.getName());
                else
                    Toast.makeText(activity, "监控未连接", Toast.LENGTH_SHORT).show();
            }
            @Override
            public void closeHelp() {
                EquipmentUtil.closeHelp(equipmentsBean.getCode());
                holder.monitorWithButton.getMonitorView().closeHelp();
            }

            @Override
            public void openCall() {
                NetWorkUtil.getNetWorkUtil().getQAType(activity, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        activity.runOnUiThread(()-> Toast.makeText(activity,"网络连接失败："+e.getMessage(),Toast.LENGTH_SHORT).show());
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String responseText = response.body().string();
                        Log.d("QAType", "onResponse: " + responseText);
                        List<QATypeBean> qaTypeBeanList = new Gson().fromJson(responseText, new TypeToken<List<QATypeBean>>() {}.getType());
                        activity.runOnUiThread(()->PopUtil.showQACall(activity,equipmentsBean,qaTypeBeanList));
                    }
                });
            }
        });
        holder.monitorWithButton.getMonitorView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(activity, MonitorActivity.class);
                intent.putExtra("equipmentsBean", new Gson().toJson(equipmentsBean));
                activity.startActivity(intent);
            }
        });
        list.add(holder);
    }

    @Override
    public int getItemCount() {
        return equipmentsBeanList.size();
    }

    public void reStart(){
        for(ViewHolder holder:list){
            holder.monitorWithButton.getMonitorUtil().reStartMonitor();
        }
    }


}
